import json
import os
import numpy as np
Вот так выглядит молекула в Discovery Studio. Сохраним в формате json и обработаем с помощью Python.

filepath = os.path.join('TestMoleculaFormat' , 'cancer_mol.json')
with open(filepath, 'r') as j:
json_data = json.load(j)
json_data.keys() , json_data['meshes'][0].keys()
vertices = np.array(json_data['meshes'][0]['vertices']).reshape((-1,3)) #считали вершины
triang_ind = np.array(json_data['meshes'][0]['indices']).reshape((-1,3)) #считали индексы вершин треугольников
normals = np.array(json_data['meshes'][0]['normals']).reshape((-1,3)) #считали векторы нормалей
colors = np.array(json_data['meshes'][0]['colors']).reshape((-1,3)) # считали цвета вершин
vertices.shape , triang_ind.shape , normals.shape , colors.shape
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
rand_vector = np.random.randint(0,normals.shape[0] , 500)
fig= plt.figure(figsize=(18, 16), dpi= 80, facecolor='w', edgecolor='k')
ax = fig.add_subplot(111, projection='3d')
ax.scatter(vertices[:,0],vertices[:,1],vertices[:,2] , color = colors)
ax.quiver(vertices[rand_vector,0],vertices[rand_vector,1],vertices[rand_vector,2],
normals[rand_vector,0] , normals[rand_vector,1] ,normals[rand_vector,2] )
def triang_colors(colors , triang_ind):
return [tuple(np.mean(colors[triang] , axis = 0)) for triang in triang_ind]
import plotly.figure_factory as ff
from plotly.offline import iplot
import plotly
fig = ff.create_trisurf(vertices[:,0],vertices[:,1],vertices[:,2],
color_func = triang_colors(colors , triang_ind),
simplices=triang_ind,
title="Molecula")
fig.show()
center = vertices.mean(axis = 0)
vectors = np.array(list(map(lambda x: x / np.linalg.norm(x) ,vertices - center)))
R = 20
fig = ff.create_trisurf(R*vectors[:,0],R*vectors[:,1],R*vectors[:,2],
color_func = triang_colors(colors , triang_ind),
simplices=triang_ind,
title="Sphere aproximation")
fig.show()
plot_data = []
mol = ff.create_trisurf(vertices[:,0],vertices[:,1],vertices[:,2],
color_func = triang_colors(colors , triang_ind),
simplices=triang_ind,
title="Molecula")
print(mol['data'])
plot_data.append(mol)
for i in range(3):
trace = ff.create_trisurf(vertices[:,0],vertices[:,1],vertices[:,2],
color_func = triang_colors(colors , triang_ind),
simplices=triang_ind,
title="Molecula")
plot_data.append(trace)
steps = list()
for i in range(3):
step = dict(
method='restyle',
args=['visible', [False] * (4)],
label='Time Step {}'.format(i)
)
step['args'][1][i] = True
steps.append(step)
sliders = [dict(steps=steps)]
layout=dict(sliders=sliders)
fig=dict(data=mol,layout=layout)
plotly.offline.plot(fig,validate=False)